<?php

class DisplayController extends DooController
{

  public function index()
  {
    $data['title'] = "Accueil";
    $data['SITE'] = "Marseille location appartements";
    $data['baseurl'] = Doo::conf()->APP_URL;
    $data['active_menu'] = 'accueil';
    Doo::loadControllerAt('FrbCmsClientController', 'frbCms');
    $cms = new FrbCmsClientController();
    $data['content'] = $cms->getContent('accueil');
    $this->view()->render('accueil', $data);
  }
  
  public function getFlatPictures()
  {
    if ($this->isAjax()) {
      if (!empty($_GET['flat'])) {
        Doo::loadControllerAt('FrbFlatClientController', 'frbFlat');
        $pictures = new FrbFlatClientController();
        $data['p'] = $pictures->getPictures($_GET['flat']);
        $data['baseurl'] = Doo::conf()->APP_URL;
        $this->view()->render("flatPictures", $data);
      }
    }
  }
  
  public function appartements()
  {
    $data['baseurl'] = Doo::conf()->APP_URL;
    Doo::loadControllerAt('FrbFlatClientController', 'frbFlat');
    $flats = new FrbFlatClientController();
    if ($flats->isFull()) {
      $data['dispo-class'] = "main-site-content-appartements-content-text-searchengine-nodispo";
      $data['dispo-text'] = "Informations : Il n'y a pas d'appartements disponibles.";
    } else {
      $data['dispo-class'] = "main-site-content-appartements-content-text-searchengine-dispo";
      $data['dispo-text'] = "Informations : Des appartements sont actuellement disponibles.
        N'hésitez pas à nous contacter !";
    }
    if ($this->isAjax()) {
      $data['page']="0";
      if (!empty($_POST['act']) && $_POST["act"] == "rand" && 
        !empty($_POST["appart"]) && is_numeric($_POST["appart"])) {
          $data["flats"] = $flats->getOne($_POST["appart"]);
        if (is_null($data["flats"])) {
          echo "Erreur lors de récupération de l'appartement";
          return;
        }
      foreach ($data['flats'] as $flatI) {
        $pic = $flats->getOnePicture($flatI->cha_id);
        $alt='Image';
        if (empty($pic)) {
          $alt = 'Aucune image disponible';
          $pic[0] = "NC";
        }
        $data['pictures'][$flatI->cha_id]['pictures'] = $pic;
        $data['pictures'][$flatI->cha_id]['alt'] = $alt;
      }
          $this->view()->render('appartementsAjax', $data);
          return;
      }
    
      $valid = false;
      if (!empty($_POST)) {
        if (isset($_POST['loyer_max'])) {
          $loyer =  (!empty($_POST['loyer_max'])) ? $_POST['loyer_max'] : '10000';
          $pageSearch = 0;
          if (isset($_POST['page_rec']) && is_numeric($_POST["page_rec"])) {
            $pageSearch = $_POST['page_rec'];
            $data['page'] = $pageSearch+1;
          }
          if (is_numeric($loyer)) {
            $where = 'cha_loy_low<='.$loyer;
            $data['flats'] = $flats->getPagination($pageSearch, 4, $where);
            if (!is_null($data['flats'])) {
              $valid = true;
            }
          }
        }
      }
    
      if ($valid) {
        foreach ($data['flats'] as $flatI) {
          $pic = $flats->getOnePicture($flatI->cha_id);
          $alt='Image';
          if (empty($pic)) {
            $alt = 'Aucune image disponible';
            $pic[0] = "NC";
          }
          $data['pictures'][$flatI->cha_id]['pictures'] = $pic;
          $data['pictures'][$flatI->cha_id]['alt'] = $alt;
        }
        $this->view()->render('appartementsAjax', $data);
      } else {
        echo 'Aucun appartements ne répond à vos critères';
      }
      unset($_POST);
      return;
    } //FIN AJAX
    $data['title'] = "Appartements";
    $data['SITE'] = "Marseille location appartements";
    $data['active_menu'] = 'appartements';
    $data['lpage'] = ceil($flats->getTotalCount()/4);
    if (!empty($_GET['page']) && is_numeric($_GET['page'])) {
      $data['link_fpage'] = ($_GET['page'] > '1' && $_GET['page'] < $data["lpage"]) 
        ? $_GET['page']-1 : '1' ;
      $data['link_lpage'] = ($_GET['page'] < $data["lpage"] && $_GET['page'] >=1) 
        ? $_GET['page']+1 : '1';
      $data['fpage'] = ($_GET['page'] >=1 && $_GET['page'] <= $data["lpage"]) 
        ? $_GET['page'] : "1" ;
      $data['flats'] = $flats->getPagination($data['fpage']-1, 4, '');
    } else {
      $data['fpage'] = 1;
      $data['flats'] = $flats->getPagination();
      $data['link_fpage'] = "1";
      $data['link_lpage'] = "2";
    }
    $data['random'] = $flats->getRandom('5');
    foreach ($data['flats'] as $flatI) {
      $pic = $flats->getOnePicture($flatI->cha_id);
      $alt='Image';
      if (empty($pic)) {
        $alt = 'Aucune image disponible';
        $pic[0] = "NC";
      }
        $data['pictures'][$flatI->cha_id]['pictures'] = $pic;
        $data['pictures'][$flatI->cha_id]['alt'] = $alt;
    }
    $this->view()->render('appartements', $data);
  }
  
  public function reglement()
  {
    $data['title'] = "Accueil";
    $data['SITE'] = "Marseille location appartements";
    $data['baseurl'] = Doo::conf()->APP_URL;
    $data['active_menu'] = 'reglement';
    Doo::loadControllerAt('FrbCmsClientController', 'frbCms');
    $cms = new FrbCmsClientController();
    $data['content'] = $cms->getContent('reglement_interieur');
    $this->view()->render('reglement', $data);
  }
  
  public function contact()
  {
    $data['baseurl'] = Doo::conf()->APP_URL;
    $data['active_menu'] = 'contact';
    Doo::loadPlugin('Form');
    $action = Doo::conf()->APP_URL.'contact';
    $form = formContact($action);
    $data['contact'] = $form->render();
    if ($this->isPost()) {
      if ($form->isValid($_POST)) {
        Doo::loadControllerAt('FrbMessageClientController', 'frbMessage');
        $message = new FrbMessageClientController();
        $message->setSender($_POST['firstname'].' '.$_POST['lastname']);
        $message->setMail($_POST['mail']);
        $message->setPhone($_POST['phone']);
        $message->setContent($_POST['message']);
        if ($message->store()) {
          echo 'true';
        } else {
          echo 'false';
        }
      } else {
          echo 'false';
      }
      unset($_POST);
      return;
    }
    
    
    if ($this->isAjax()) {
      $this->render('contactAjax', $data);
    } else {
      $data['title'] = "Accueil";
      $data['SITE'] = "Marseille location appartements";
      $this->render('contact', $data);
    }
  }
  
  public function plan()
  {
    $data['title'] = "Accueil";
    $data['SITE'] = "Marseille location appartements";
    $data['baseurl'] = Doo::conf()->APP_URL;
    $data['active_menu'] = 'plan';
    $this->view()->render('plan', $data);
  }
  
  public function mentions()
  {
    $data['title'] = "Mentions légales";
    $data['SITE'] = "Marseille location appartements";
    $data['baseurl'] = Doo::conf()->APP_URL;
    $data['active_menu'] = 'accueil';
    $this->view()->render('mentions', $data);
  }
  
  private function isPost() 
  {
    if ($_SERVER['REQUEST_METHOD'] == "POST") return true;
    else return false;
  }
  
  public function uploadImg()
  {
    $data['cha_id'] = (empty($_GET['id'])) ? 0 : $_GET['id'];
    $data['pic'] = '';
    $data['error'] = "";
    if ($this->isPost()) {
      if (!empty($_POST['action-upload'])) {
        Doo::loadHelper('DooGdImage');
        $path = Doo::conf()->SITE_PATH.'global/img/appartements/pictures/';
        $gd = new DooGdImage($path, $path);
        if ($gd->checkImageExtension('profile_pic', array('jpg', 'jpeg'))) {    
          $uploadImage = uniqid(rand());
          $uploadImg = $gd->uploadImage('profile_pic',$uploadImage);
          
          $gd->generatedQuality = 100;
          $gd->generatedType="jpg";
          $newName = $gd->adaptiveResize($uploadImg, 132, 100);
          
          $gd->thumbSuffix = '_big';
          $newName2 = $gd->ratioResize($uploadImg, 400, null);
          
          unlink($path.$uploadImg);
          rename($newName, $path.'thumbnails/'.$uploadImage.'.jpg');
          rename($newName2, $path.$uploadImage.'.jpg');
          
          Doo::loadModelAt('FrbFlatPictures', 'frbFlat');
          $picture = new FrbFlatPictures();
          $picture->cha_id = $_POST['cha_id'];
          $picture->cha_pic = $uploadImage.'.jpg';
          
          $this->db()->insert($picture);
          $data['cha_id'] = $_POST['cha_id'];
          $data['pic'] = $uploadImage.'.jpg';
        } else {
          $data['error'] = 'Format du fichier incorrect';
        }
      }
    }
    $data['baseurl'] = Doo::conf()->APP_URL;
    $this->view()->render('upload-form', $data);
  }
}
